package com.bum.api.core.util.load;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.commons.lang.StringUtils;

/**
 * http://www.java.happycodings.com/Java_Util_Package/code13.html
 */
public class FileUtil {
	private static final int kFILEBUFSIZE = 1024;

	public static String read(String fileName) throws IOException {
		if (StringUtils.isBlank(fileName))
			throw new IllegalArgumentException("supplied filename to FileUtil was null");
		return read(new FileReader(fileName));
	}

	public static String read(File file) throws IOException {
		if (file == null)
			throw new IllegalArgumentException("supplied File object to FileUtil was null");
		return read(new FileReader(file));
	}

	public static String read(Reader reader) throws IOException {
		if (reader == null)
			throw new IllegalArgumentException("supplied Reader object to FileUtil was null");

		try {
			StringWriter sw = new StringWriter();
			char[] text = new char[kFILEBUFSIZE];
			int n;
			while ((n = reader.read(text, 0, kFILEBUFSIZE)) > 0)
				sw.write(text, 0, n);

			return sw.toString();
		} finally {
			if (reader != null)
				reader.close();
		}
	}

	public static void write(String fileName, String contents) throws IOException {
		if (StringUtils.isBlank(fileName) || StringUtils.isBlank(contents))
			throw new IllegalArgumentException("supplied filename and content to FileUtil should not blank");
		write(new FileWriter(fileName), contents);
	}
	
	public static void write(File file, String contents) throws IOException {
		if (file == null || StringUtils.isBlank(contents))
			throw new IllegalArgumentException("supplied filename and content to FileUtil should not blank");
		write(new FileWriter(file), contents);
	}

	static void write(Writer fileWriter, String contents) throws IOException {
		if (fileWriter == null || StringUtils.isBlank(contents))
			throw new IllegalArgumentException("supplied FileWriter and content to FileUtil should not blank");

		try {
			StringReader sr = new StringReader(contents);
			char[] text = new char[kFILEBUFSIZE];
			int n;
			while ((n = sr.read(text, 0, kFILEBUFSIZE)) > 0)
				fileWriter.write(text, 0, n);
		} finally {
			if (fileWriter != null)
				fileWriter.close();
		}
	}
	
	public static void append(String fileName, String contents) throws IOException {
		if (StringUtils.isBlank(fileName) || StringUtils.isBlank(contents))
			throw new IllegalArgumentException("supplied filename and content to FileUtil should not blank");
		append(new FileWriter(fileName, true), contents);
	}
	
	public static void append(File file, String contents) throws IOException {
		if (file == null || StringUtils.isBlank(contents))
			throw new IllegalArgumentException("supplied filename and content to FileUtil should not blank");
		append(new FileWriter(file, true), contents);
	}
	
	static void append(Writer fileWriter, String contents) throws IOException {
		if (fileWriter == null || StringUtils.isBlank(contents))
			throw new IllegalArgumentException("supplied FileWriter and content to FileUtil should not blank");
		
		try {
			StringReader sr = new StringReader(contents);
			 
			char[] text = new char[kFILEBUFSIZE];
			int n;
			while ((n = sr.read(text, 0, kFILEBUFSIZE)) > 0) {
				StringBuffer strBuffer = new StringBuffer(n);
				strBuffer.append(text);
				fileWriter.append(strBuffer, 0, n);
			}
		} finally {
			if (fileWriter != null)
				fileWriter.close();
		}
	}
}
